package javaSE;

import java.util.Arrays;
import java.util.Locale;

public class StringTest {
    public static void main1(String[] args) {
        //构造String的方式
        String str = "abcdef";
        String str1 = new String("hello");

        //将一个字符串数组转换成了字符串
        char[] chars = {'a', 'b', 'c'};
        String str2 = new String(chars);
        System.out.println(str2);
    }

    public static void main2(String[] args) {
        //不是说传引用就能改变实参的值，你要看，这个引用到底干啥了
        //不能通过改变str来修改str1的值
        String str = "asdfgv";
        String str1 = str;
        System.out.println(str);
        System.out.println(str1);
        System.out.println("====================");
        str = "asdfff";
        System.out.println(str);
        System.out.println(str1);
    }

    public static void main3(String[] args) {
        String str1 = new String("1") + new String("1");
        str1.intern();//*****手动入池*****
        String str2 = "11";
        System.out.println(str1 == str2);
    }

    public static void main4(String[] args) {
        String str1 = "11";
        String str2 = null;
        System.out.println(str1.equals(str2));//不会空指针报错
    }

    public static void main5(String[] args) {
        //数组的整体赋值只有一次机会 就是在定义的时候    字符串的不可变
        final int[] array = {1, 2, 3, 4, 5};
        // array = new int[]{4,5,6};
    }

    public static void main6(String[] args) {
        //整个数组变成字符串
        /*char[] value = {'a','b','c','d'};
        String str = new String(value);
        System.out.println(str);*/

        //数组部分变成字符串
        char[] value = {'a', 'b', 'c', 'd'};
        String str = new String(value, 1, 3);
        System.out.println(str);

        //取出字符串的某个字符
        String str1 = "hello";
        char ch = str1.charAt(2);
        System.out.println(ch);

        //整个字符串变成数组
        char[] vc = str1.toCharArray();
        System.out.println(Arrays.toString(vc));//打印数组
    }

    public static void main8(String[] args) {
        //将字节转换成字符串
        byte[] bytes = {97, 98, 99, 100};
        String str = new String(bytes);
        System.out.println(str);

        //将字符串变成字节
        String str1 = "bit";
        byte[] bytes1 = str1.getBytes();
        System.out.println(Arrays.toString(bytes1));
    }


    //字符串的比较
    public static void main9(String[] args) {
        String str1 = "asdfgh";
        String str2 = "zxcvbn";
        System.out.println(str1.equals(str2));//比较

        String str3 = "asdfgh";
        String str4 = "ASDFGH";
        System.out.println(str3.equalsIgnoreCase(str4));//比较大小，忽略字母大小写

        String str5 = "abc";
        String str6 = "ABC";
        int ret = str5.compareTo(str6);//通过相减得正负判断大小
        System.out.println(ret);

        String str7 = "abacabcd";
        String str8 = "abc";
        boolean flg = str7.contains(str8);//是否包含
        System.out.println(flg);

        String str9 = "abacabcd";
        String str10 = "abc";          //fromindex:从哪个位置找
        int index = str9.indexOf(str10, 2);//在主串当中找到字串出现的位置 类似于C得struct->KMP算法
        System.out.println(index);      //lastindexof:从后往前找
        System.out.println(str9.startsWith("a"));//判断是否是以它开头
        //endsWith 以什么结尾的
    }

    //替换replace
    public static void main10(String[] args) {
        String str = "addadadadfffsw";
        String ret = str.replace('d', 'b');//将d替换为b  也可以选择其他替换模式
        System.out.println(ret);
    }

    //分割
    public static void main11(String[] args) {
        /*String str = "name=zhangsan&age=19";
        String[] strings = str.split("&");
        for (String s:strings){
            System.out.println(s);
        }*/

        //特殊情况
        String str = "192.168.1.0";
        String[] strings = str.split("\\.", 2);//特殊情况，需要加上转义字符   给2分两组
        for (String s : strings) {
            System.out.println(s);
            System.out.println("=======================================");

            String str1 = "java20 69&125#hello";
            String[] strings1 = str1.split(" |&|#");//从每一个分隔符
            for (String s1 : strings1) {
                System.out.println(s1);
            }
        }
    }

    //字符串的截取
    public static void main12(String[] args) {
        String str = "asdcfv";
        //String ret = str.substring(2);//从下标为2处提取
        String ret = str.substring(2, 4);//左闭右开
        System.out.println(ret);
    }

    public static void main13(String[] args) {
        String str = "     abc     def    ";
        String ret = str.trim();
        System.out.println(ret);//去除两边的空格，中间不会
    }

    public static void main14(String[] args) {
        String str = "abcdefBFGH";
        String ret = str.toUpperCase(Locale.ROOT);//转大写，也有转小写
        System.out.println(ret);
    }

    public static void main15(String[] args) {
        String str = "adfdsa";
        String ret = str.concat("bitt");//拼接    拼接的结果不会入池
        System.out.println(ret);
    }


    //    public boolean isNumeberChar(String s) {
//        for (int i = 0; i < s.length(); i++) {
//            char ch = s.charAt(i);
//            //判断某个字符是不是数字
//            boolean flg = Character.isDigit(ch);// *******Character*********
//            if (flg == false){
//                return false;
//            }
////            if (ch < '0' || ch > '9') {
////                return false;
////            }
//        }
//        return true;
//    }
//
//    public static void main7(String[] args) {
//        String str = "123456";
//        System.out.println(isNumberChar(str));
//    }
//}

    //synchronized:保证线程安全，会加锁
    //*******************StringBuffer和StringBuilder
    //StringBuffer采用同步处理，也会加锁解锁，属于线程安全操作；而StringBuilder未采用同步处理，属于线程不安全操作
    //String和StringBuffer最大的区别在于：String的内容无法修改，而StringBuffer的内容可以修改。频繁修改字符串的情况考虑使用StingBuffer。
    //普通的String拼接会被优化为StringBuilder...append
    public static void main16(String[] args) {
        /*StringBuilder sb = new StringBuilder();
        sb.append("adsfss").append("125634");;//append不会产生新的对象，还是原来这个对象
        System.out.println(sb);*/

        String str = "asdfgvb";
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        for (int i = 0; i <= 9; i++) {
            sb.append(i);
            str = sb.toString();
        }
        System.out.println(str);

        StringBuffer sb2 = new StringBuffer("asdfgh");//
        sb.reverse();//倒置
        System.out.println(sb2);
    }


    //String转换成Stringbuilder  利用StringBuilder的构造方法或append()方法
    public static StringBuilder main17(String[] args) {
        /*String str = "aaasdf";
        return new StringBuilder(str);*/

        String str = "aaasdf";
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        return sb;
    }

    //Stringbuilder转换成String    调用toString方法
    public static void main(String[] args) {
        StringBuilder str = new StringBuilder("asdfsa");
        str.toString();
        System.out.println(str);
    }



}
